<?php
namespace Crm\Controller;

use Zend\View\Model\ViewModel;
use Endroid\QrCode\QrCode;
use Crm\Common\CategoryHelper;
use Crm\Common\CommonHelper;

/**
 * 商户管理
 */
class OrgController extends AbstractController
{
    /**
     * 商户列表
     */
    public function listAction()
    {  
        $city = $this->getCityTable()->findAll();
        $catalog = $this->getCatalogTable()->findAll();

        return new ViewModel(array(
            'city' => $city,
            'catalog' => $catalog
        ));
    }

    public function ajaxOrgListAction()
    {
        // $request = $this->getRequest()->getQuery();
        $request = $this->request->getPost()->toArray();
        
        (int) $catalog_id = isset($request['catalog_id']) ? $request['catalog_id'] : 0;
        (int) $city_id = isset($request['city_id']) ? $request['city_id'] : 0;
        $org_name = isset($request['org_name']) ? $request['org_name'] : '';
        
        $org_is_direct_sale = isset($request['org_is_direct_sale']) ? $request['org_is_direct_sale'] : '';
        
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        
        //指定管理 本人的信息
        //$admin_org_id = !empty($_SESSION['account']['org_id']) ? $_SESSION['account']['org_id'] : '';
        
        $query = array(
            'catalog_id' => $catalog_id,
            'city_id' => $city_id,
            'org_name' => $org_name,
            'org_is_direct_sale' => $org_is_direct_sale,
            'page' => $page,
            'admin_org_id' => $this->adminOrgId
        );
        
        $paginator = $this->getOrganizationTable()->find($query);

        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        
        return $viewModel;
    }
    
    /**
     * 生成商户二维码
     */
    public function ajaxQrcodeAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['org_id'];
        
        $domain = $this->getConfigSys()['qrcode_domain'];
        
        $qrcode_url = $domain.'/organization/'.$id;
        $qrCode = new QrCode();
        $qrCode ->setText($qrcode_url)
        ->setSize(300)
        ->setPadding(10)
        ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
        ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
        //->setLabel('Scan the code')
        ->setLabelFontSize(16)
        ->setImageType(QrCode::IMAGE_TYPE_PNG)
        ->setErrorCorrection(QrCode::LEVEL_MEDIUM);
        
        //"/uploads/qrcode/".$id.".png"
        //$qrCode->get('png');
        header('Content-Type: '.$qrCode->getContentType());
        $qrCode->render();
        
        //$response = new Response($qrCode->get(), 200, array('Content-Type' => $qrCode->getContentType()));
        
        die;
    }

    /**
     * 新增商户
     */
    public function addAction()
    {
        $catalog = $this->getCatalogTable()->findAll();
        return new ViewModel(array(
            'catalog' => $catalog
        ));
    }

    public function insertAction()
    {
        $request = $this->getRequest();
        
            if ($request->isPost()) {
                $data = $this->request->getPost()->toArray();

                //WU_FILE_0 商户展示图片 
                $details_img = array_slice($data, 28);
               
                $query = array(
                'org_name' => $data['org_name'],
                'org_title' => $data['org_title'],
                'org_subtitle' => $data['org_subtitle'],
                'org_advice' => $data['org_advice'],
                'org_level' => $data['org_level'],
                'catalog_id' => $data['catalog_id'],
                'org_contact_name' => $data['org_contact_name'],
                'org_description' => $data['org_description'],
                'org_logo' => $data['org_logo'],
                'org_thumb_image' => $data['org_thumb_image'],
                'org_features' => $data['org_features'],
                'org_live' => $data['org_live'],
                'org_is_open' => $data['org_is_open'],
                'org_next_notice' => $data['org_next_notice'],
                'org_type' => $data['org_type'],
                'org_is_display' => $data['org_is_display'],
                'org_other' => $data['org_other'],
                'org_is_direct_sale' => $data['org_is_direct_sale']
                
            );
                
            //地址 和 地址图片
            $org_address = array(
                'address_name' => $data['org_address'],
                'address_img'  => $data['address_img']
            );
            
            $query['org_address'] = json_encode($org_address);
                
            $query['org_details'] = json_encode($details_img);
            
            //营业执照
            if(isset($data['org_img1'])){
                $query['org_img1'] = $data['org_img1'];
            }

            if(isset($data['org_img2'])){
                $query['org_img2'] = $data['org_img2'];
            }

            if(isset($data['org_img3'])){
                $query['org_img3'] = $data['org_img3'];
            }

            if(isset($data['org_img4'])){
                $query['org_img4'] = $data['org_img4'];
            }

            if(isset($data['org_img5'])){
                $query['org_img5'] = $data['org_img5'];
            }

            if(isset($data['org_img6'])){
                $query['org_img6'] = $data['org_img6'];
            }

            if(isset($data['org_img7'])){
                $query['org_img7'] = $data['org_img7'];
            }
            
            //暂未用
            if(isset($data['org_img8'])){
                $query['org_img8'] = $data['org_img8'];
            }

            
            $result = $this->getOrganizationTable()->create($query);

             if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("保存成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'list'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("保存失败！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'add'
                ));
            }


        }
  
    }
    
    /**
     * 商户详情
     */
    public function detailsAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['org_id'];
        $org = $this->getOrganizationTable()->findOne($id);
        
        $org_address = json_decode($org['org_address'],true);
        $org_details = json_decode($org['org_details'],true);
    
        $catalog = $this->getCatalogTable()->findAll();
        return new ViewModel(array(
            'catalog' => $catalog,
            'org' => $org,
            'org_address' => $org_address,
            'org_details' => $org_details
        ));
    }
    
    public function editAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['org_id'];
        $org = $this->getOrganizationTable()->findOne($id);
 
        $org_address = json_decode($org['org_address'],true);
        $org_details = json_decode($org['org_details'],true);

        $catalog = $this->getCatalogTable()->findAll();
        return new ViewModel(array(
            'catalog' => $catalog,
            'org' => $org,
            'org_address' => $org_address,
            'org_details' => $org_details
         ));
    }

    public function updateAction()
    {


        $request = $this->getRequest();
        
            if ($request->isPost()) {
                $data = $this->request->getPost()->toArray();

                
                $query = array(
                'org_name' => $data['org_name'],
                'org_title' => $data['org_title'],
                'org_subtitle' => $data['org_subtitle'],
                'org_advice' => $data['org_advice'],
                'org_level' => $data['org_level'],
                'catalog_id' => $data['catalog_id'],
                //'org_contact' => $data['org_contact'],
                'org_description' => $data['org_description'],
                'org_logo' => $data['org_logo'],
                'org_thumb_image' => $data['org_thumb_image'],
                'org_features' => $data['org_features'],
                'org_live' => $data['org_live'],
                'org_is_open' => $data['org_is_open'],
                'org_next_notice' => $data['org_next_notice'],
                'org_type' => $data['org_type'],
                'org_is_display' => $data['org_is_display'],
                'org_is_direct_sale' => $data['org_is_direct_sale']
            );

            //地址 和 地址图片
            $org_address = array(
                'address_name' => $data['org_address'],
                'address_img'  => $data['address_img']
            );
            $query['org_address'] = json_encode($org_address);
            
            //WU_FILE_0 商户展示图片
            $details_img = array_slice($data, 29);
            $query['org_details'] = json_encode($details_img);
            
            //营业执照
            if(isset($data['org_img1'])){
                $query['org_img1'] = $data['org_img1'];
            }
            
            if(isset($data['org_img2'])){
                $query['org_img2'] = $data['org_img2'];
            }
            
            if(isset($data['org_img3'])){
                $query['org_img3'] = $data['org_img3'];
            }
            
            if(isset($data['org_img4'])){
                $query['org_img4'] = $data['org_img4'];
            }
            
            if(isset($data['org_img5'])){
                $query['org_img5'] = $data['org_img5'];
            }
            
            if(isset($data['org_img6'])){
                $query['org_img6'] = $data['org_img6'];
            }
            
            if(isset($data['org_img7'])){
                $query['org_img7'] = $data['org_img7'];
            }
            
            //暂未用
            if(isset($data['org_img8'])){
                $query['org_img8'] = $data['org_img8'];
            }  
           

            (int) $id = $data['org_id'];
            
            $result = $this->getOrganizationTable()->updateById($query, $id);
             if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'list'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("编辑失败！");
                return $this->redirect()->toUrl("list?org_id=$id");
            }


        }
  
    
    }
    
    public function removeAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['org_id'];
        
        if ($id == 0) {
            $this->flashMessenger()->addErrorMessage("参数有误，请重新删除");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'list'
            ));
        }
        
        $result = $this->getOrganizationTable()->remove($id);
        
        if ($result) {
            
            $this->flashMessenger()->addSuccessMessage("删除成功！");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'list'
            ));
        } else {
            
            $this->flashMessenger()->addErrorMessage("删除失败！");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'list'
            ));
        }
    }

    
    //start -----------------------------------------------------------------
    
    public function catelogListAction()
    {
        return new ViewModel();
    }

    public function ajaxCatelogListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        $catalog_name = isset($request['catalog_name']) ? $request['catalog_name'] : '';
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'catalog_name' => $catalog_name,
            'page' => $page
        );
        
        $paginator = $this->getCatalogTable()->find($query);
        
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        
        return $viewModel;
    }

    public function addCatelogAction()
    {
        $city = $this->getCityTable()->findAll();
        
        return new ViewModel(array(
            'city' => $city
        ));
    }

    public function insertCatelogAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $city_id = $request->getPost("city_id");
            $cate_name = $request->getPost("cate_name");
            
            $data = array(
                'city_id' => $city_id,
                'catalog_name' => $cate_name
            );
            
            $result = $this->getCatalogTable()->create($data);
            if ($result) {
                $response = array(
                    'reason' => '新增成功！',
                    'result' => true
                );
            } else {
                
                $response = array(
                    'reason' => '新增失败！',
                    'result' => false
                );
            }
            
            die(json_encode($response));
        } else {
            
            $response = array(
                'reason' => '非法请求',
                'result' => false
            );
            die(json_encode($response));
        }
    }

    public function editCatelogAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['catalog_id'];
        $catalog = $this->getCatalogTable()->findOne($id);
        
        $city = $this->getCityTable()->findAll();
        
        return new ViewModel(array(
            'catalog' => $catalog,
            'city' => $city
        ));
    }

    public function updateCatelogAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            
            $data1 = array(
                'catalog_id' => ''
            );
            $catelog_data = array_diff_key($data, $data1);
            
            (int) $id = $data['catalog_id'];
            $result = $this->getCatalogTable()->updateById($catelog_data, $id);
            
            if ($result) {
                
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'catelogList'
                ));
            } else {
                
                $this->flashMessenger()->addErrorMessage("编辑失败！");
                return $this->redirect()->toUrl("editCatelog?catalog_id=$id");
            }
        }
    }

    public function removeCatelogAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['catalog_id'];
        
        if ($id == 0) {
            $this->flashMessenger()->addErrorMessage("参数有误，请重新删除");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'catelogList'
            ));
        }
        // 是否有子分类
        $has_data = $this->getOrganizationTable()->findByCatalogId($id);
        
        if ($has_data) {
            
            $this->flashMessenger()->addErrorMessage("该分类下有数据，如删除请先删除该分类下所有数据！");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'catelogList'
            ));
        }
        
        $result = $this->getCatalogTable()->remove($id);
        
        if ($result) {
            
            $this->flashMessenger()->addSuccessMessage("删除成功！");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'catelogList'
            ));
        } else {
            
            $this->flashMessenger()->addErrorMessage("删除失败！");
            return $this->redirect()->toRoute('crm/org', array(
                'action' => 'catelogList'
            ));
        }
    }

    public function cateListAction()
    {
        return new ViewModel();
    }
    
    public function ajaxCateListAction()
    {
        // $request = $this->getRequest()->getQuery();
        $request = $this->request->getPost()->toArray();
    
        $category_name = isset($request['category_name']) ? $request['category_name'] : '';
   
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        $query = array(
            'category_name' => $category_name,
            'page' => $page
        );
        $paginator = $this->getCategoryTable()->find($query);
    
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
    
        return $viewModel;
    }
    
    public function addCateAction()
    {
        $org = $this->getOrganizationTable()->findAll();
    
        return new ViewModel(array(
            'org' => $org
        ));
    }
    
    public function insertCateAction()
    {
        $request = $this->getRequest();
    
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            $result = $this->getCategoryTable()->create($data);
            if ($result) {
    
                $this->flashMessenger()->addSuccessMessage("保存成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'cateList'
                ));
            } else {
    
                $this->flashMessenger()->addErrorMessage("保存失败！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'addCate'
                ));
            }
        }
    }
    
    public function editCateAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['category_id'];
        $category = $this->getCategoryTable()->findOne($id);
    
        $organization = $this->getOrganizationTable()->findAll();
        return new ViewModel(array(
            'organization' => $organization,
            'category' => $category
        ));
    }
    
    public function updateCateAction()
    {
        $request = $this->getRequest();
    
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
    
            $data1 = array(
                'category_id' => ''
            );
            $cate_data = array_diff_key($data, $data1);
    
            (int) $id = $data['category_id'];
            $result = $this->getCategoryTable()->updateById($cate_data, $id);
    
            if ($result) {
    
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/product', array(
                    'action' => 'cateList'
                ));
            } else {
    
                $this->flashMessenger()->addErrorMessage("编辑失败！");
    
                return $this->redirect()->toUrl("cateList?category_id=$id");
            }
        }
    }
    
    
    public function insertCityAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            
            $city_name = $request->getPost("city_name");
            
            $data = array(
                'city_name' => $city_name
            );
            $result = $this->getCityTable()->create($data);
            if ($result) {
                $response = array(
                    'reason' => '新增成功！',
                    'result' => true
                );
            } else {
                
                $response = array(
                    'reason' => '新增失败！',
                    'result' => false
                );
            }
            
            die(json_encode($response));
        } else {
            
            $response = array(
                'reason' => '非法请求',
                'result' => false
            );
            die(json_encode($response));
        }
    }
    
    //end 暂未用-----------------------------------------------------------------
    
 
    
    /**
     * AJAX请求 商户地址数据
     */
    public function ajaxAddressListAction()
    {
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['org_id'];
        $response_res = $this->getOrganizationAddrTable()->findAllByOrgId($id);
        
        $cates = CategoryHelper::unlimitedForLevel($response_res);
       
        $html_content = '';
        //<td  colspan="2"><a href="javascript:editAddress( '.$v['id']. ');">修改</a></td>
        if($cates){
            $html_content .= '<td calss="details"  colspan="11">';
            $html_content .= '<table id="address-list-show" class="table table-hover">';
            $html_content .= '<tbody>';
            foreach($cates as $v){
                $html_content .= '<tr>';
                $html_content .= '<td style="text-align:left" class="edit-address" data-address-id="'.$v['id']. '" data-address-html-interval="'.$v['html']. '">' .$v['html'].$v['title']. ' </td>';
                $html_content .= '</tr>';
            }
            $html_content .= ' </tbody>';
            $html_content .= '</table>';
            $html_content .= ' </td>';
        
        }
        die($html_content);
        
    }
    
    /**
     * 商户地址列表
     */
    public function addAddressAction(){
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['org_id'];
        $org = $this->getOrganizationTable()->findOne($id);
        
        $response_res = $this->getOrganizationAddrTable()->findAllByOrgId($id);
  
        $cates = CategoryHelper::unlimitedForLayer($response_res, $name = 'child', $pid = 0);
        
        return new ViewModel(array(
            'org' => $org,
            'cat_select' => $cates
        ));
    }
    
    
    /**
     * 商户地址列表
     */
    public function insertAddressAction()
    {

        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            $result = $this->getOrganizationAddrTable()->create($data);
        
            if ($result) {
        
                $this->flashMessenger()->addSuccessMessage("新增成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'list'
                ));
            } else {
        
                $this->flashMessenger()->addErrorMessage("新增失败！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'add-address'
                ));
            }
        }  
        
    }
    
    
    /**
     * 编辑商户地址入库
     */
    public function updateAddressAction(){
        

        $request = $this->getRequest();
        
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['id'];
        $title = CommonHelper::safe($request['value']);
        //if ($request->isGet()) {
        
            //$id = $request->getPost("id");
           // $title = $request->getPost("value");
        
            $data = array(
                'title' => $title,
                'id' => $id
            );
            $result = $this->getOrganizationAddrTable()->updateById($data, $id);
            
            if ($result) {
                $response = array(
                    'reason' => '编辑成功！',
                    'result' => true
                );
            } else {
        
                $response = array(
                    'reason' => '编辑失败！',
                    'result' => false
                );
            }

//         } else {
        
//             $response = array(
//                 'reason' => '非法请求',
//                 'result' => false
//             );
            
//         }
        
        die(json_encode($response));
        
    }
    
    
    /**
     * 商户商品列表
     */
    public function goodsListAction(){
       $organization = $this->getOrganizationTable()->findAllDisplay(2);
        $good_cate = $this->getCategoryTable()->findAllDisplay();
        return new ViewModel(array(
            'organization' => $organization,
            'good_cate' => $good_cate
        ));
    }
    
    /**
     * AJAX请求 商户商品数据
     */
    public function ajaxGoodsListAction()
    {
        $request = $this->request->getPost()->toArray();
        
        (int) $org_id = isset($request['org_id']) ? $request['org_id'] : 0;
        $product_name = isset($request['product_name']) ? $request['product_name'] : '';
        (int) $page = isset($request['page']) ? $request['page'] : 1;
        
        //指定管理 本人的信息
        //$admin_org_id = !empty($_SESSION['account']['org_id']) ? $_SESSION['account']['org_id'] : '';
        
        $query = array(
            'org_id' => $org_id,
            'product_name' => $product_name,
            'page' => $page,
            'admin_org_id' => $this->adminOrgId
        );
        
        $paginator = $this->getProductTable()->findOrgGoods($query);
        $result = array(
            'paginator' => $paginator
        );
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)->setTerminal(true);
        return $viewModel;
    }
    
    /**
     * 新增商户商品
     */
    public function addGoodsAction(){
        
        $organization = $this->getOrganizationTable()->findAllDisplay(1 , $this->adminOrgId);
        //$good_cate = $this->getCategoryTable()->findAllDisplay($this->adminOrgId);
        
        return new ViewModel(array(
            'organization' => $organization
            //'good_cate' => $good_cate
        ));
    }
    
    
    /**
     * 商户商品入库
     */
    public function insertGoodsAction(){
    $request = $this->getRequest();
    
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
            // unset($data['file']);
            // array_diff($data,);
    
            $data1 = array(
                'file' => '',
                'category_id' => ''
            );
            $goods_data = array_diff_key($data, $data1);
            $goods_data['product_type'] = 1; 
            $result = $this->getProductTable()->create($goods_data);
    
            if ($result) {
    
                $this->flashMessenger()->addSuccessMessage("保存成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'goods-list'
                ));
            } else {
    
                $this->flashMessenger()->addErrorMessage("保存失败！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'add-goods'
                ));
            }
        }
    }
    
    /**
     * 复制商品
     */
    public function copyGoodsAction(){
        
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['product_id'];
        $data = $this->getProductTable()->findOneToArray($id);
        
        
       
        $data1 = array(
            'product_id' => ''
        );
        $goods_data = array_diff_key($data[0], $data1);
        
  
        
        $result = $this->getProductTable()->create($goods_data);
        
        if ($result) {
            $this->flashMessenger()->addSuccessMessage("复制成功！");
        } else {
            $this->flashMessenger()->addErrorMessage("复制失败！");
        }
        
        return $this->redirect()->toRoute('crm/org', array(
            'action' => 'goods-list'
        ));
        
        
    }
    
    /**
     * 编辑商户商品
     */
    public function editGoodsAction(){
        
        $request = $this->getRequest()->getQuery();
        (int) $id = $request['product_id'];
        $product = $this->getProductTable()->findOne($id);
        
        //$organization = $this->getOrganizationTable()->findAllDisplay(1);
        //$good_cate = $this->getCategoryTable()->findAllDisplay();
        
        $organization = $this->getOrganizationTable()->findAllDisplay(1 , $this->adminOrgId);
    
        return new ViewModel(array(
            'organization' => $organization,
            //'good_cate' => $good_cate,
            'product' => $product
        ));
    
    }
    
    
    /**
     * 编辑商户商品入库
     */
    public function updateGoodsAction(){
    
        $request = $this->getRequest();
    
        if ($request->isPost()) {
            $data = $this->request->getPost()->toArray();
    
            $data1 = array(
                'file' => '',
                'category_id' => '',
                'product_id' => ''
            );
    
            $product_data = array_diff_key($data, $data1);
    
            (int) $id = $data['product_id'];
            $result = $this->getProductTable()->updateById($product_data, $id);
    
            if ($result) {
    
                $this->flashMessenger()->addSuccessMessage("编辑成功！");
                return $this->redirect()->toRoute('crm/org', array(
                    'action' => 'goods-list'
                ));
            } else {
    
                $this->flashMessenger()->addErrorMessage("编辑失败！");
    
                return $this->redirect()->toUrl("goods-list?product_id=$id");
            }
        }
    
    }
    
    public function sellGoodsAction(){
        //商品列表
        $org_id = isset($_GET['org_id']) ? $_GET['org_id'] : $this->adminOrgId;
        $goods = $this->getProductTable()->findAllByOrgId($org_id);
        
        //$request = $this->getRequest()->getQuery();
        //(int) $id = $request['org_id'];
        //$org = $this->getOrganizationTable()->findOne($id);
        
        return new ViewModel(array(
            'goods' => $goods,
            'select_week' => CommonHelper::orgSelectWeek()
            //'org' => $org
        ));
    }
    
    
    public function ajaxSellGoodsAction(){
    
    
        //'add_type':addType,'goods_id':goodsId,'sell_data':sellData
    
        $request = $this->getRequest();
    
        if ($request->isPost()) {
    
            $add_type = $request->getPost("add_type");
            $goods_id = $request->getPost("goods_id");
            $sell_data = $request->getPost("sell_data");
    
            $data = array(
                'type' => $add_type,
                'product_id' => $goods_id,
                $sell_data => 1
    
            );
            $result = $this->getMealorderTable()->create($data);
            if ($result) {
                $response = array(
                    'reason' => '新增成功！',
                    'result' => true
                );
            } else {
    
                $response = array(
                    'reason' => '新增失败！',
                    'result' => false
                );
            }
        } else {
    
            $response = array(
                'reason' => '非法请求',
                'result' => false
            );
    
        }
    
        die(json_encode($response));
    
    
    }
    
    /**
     * 查询选中不同类型的商品
     */
    public function ajaxSelectSellGoodsAction(){

//         $sell_data = 's2';

//         $result1 = $this->getMealorderTable()->findAllByDataAndType(3, $sell_data);
        
//         var_dump($result1);
//         die;

        $request = $this->getRequest();
        
        if ($request->isPost()) {

            $sell_data = $request->getPost("sell_data");
            (int) $org_id = $request->getPost("org_id");
            $org_id = isset($org_id) ? $org_id : $this->adminOrgId;
            $result1 = $this->getMealorderTable()->findAllByDataAndType(1, $sell_data, $org_id);
            
            $result2 = $this->getMealorderTable()->findAllByDataAndType(2, $sell_data, $org_id);
            
            $result3 = $this->getMealorderTable()->findAllByDataAndType(3, $sell_data, $org_id);
            
            $html1 = '';
            $html2 = '';
            $html3 = '';
            
            if($result1){
                foreach ($result1 as $v){
                
                    $html1.= '<div class="tab-item">' . $v['product_name'] . '<a href="javascript:;" class="btn-del" data-goods-type="1" data-goods-id="' . $v['product_id'] . '">X</a></div>';
                
                }
            }
            
            if($result2){
                foreach ($result2 as $v){
                
                    $html2.= '<div class="tab-item">' . $v['product_name'] . '<a href="javascript:;" class="btn-del" data-goods-type="2" data-goods-id="' . $v['product_id'] . '">X</a></div>';
                
                }
            }
            
            if($result3){
                foreach ($result3 as $v){
                
                    $html3.= '<div class="tab-item">' . $v['product_name'] . '<a href="javascript:;" class="btn-del" data-goods-type="3" data-goods-id="' . $v['product_id'] . '">X</a></div>';
                
                }
            }
            
            
            $response = array(
                                'reason' => '成功',
                                'result' => true,
                                'html1' => $html1,
                                'html2' => $html2,
                                'html3' => $html3
                            );

        } else {
        
            $response = array(
                'reason' => '非法请求',
                'result' => false
            );
            
        }
        
        die(json_encode($response));
        
        
    }
    
    public function ajaxDelSellGoodsAction(){
    
    
        //'add_type':addType,'goods_id':goodsId,'sell_data':sellData
    
        $request = $this->getRequest();
    
        if ($request->isPost()) {
    
            $add_type = $request->getPost("add_type");
            $goods_id = $request->getPost("goods_id");
            $sell_data = $request->getPost("sell_data");
    
            $data = array(
                'type' => $add_type,
                'product_id' => $goods_id,
                $sell_data => 1
    
            );
            $result = $this->getMealorderTable()->remove($data);
            if ($result) {
                $response = array(
                    'reason' => '删除成功！',
                    'result' => true
                );
            } else {
    
                $response = array(
                    'reason' => '删除失败！',
                    'result' => false
                );
            }
        } else {
    
            $response = array(
                'reason' => '非法请求',
                'result' => false
            );
    
        }
    
        die(json_encode($response));
    
    
    
    
    }
    
    
}